package com.boot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.entity.TUser;
import com.boot.service.IUserService;
import com.boot.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/15 10:09
 * @description : 做mybatis-plus基本使用的演示
 */
@RestController
public class TestController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 根据ID获取用户信息
     * @CreateTime 2019/6/8 16:34
     * @Param  userId  用户ID
     * @Return TUser 用户实体
     */
    @RequestMapping("/getInfo")
    public TUser getInfo(Long userId){





        TUser TUser = userService.getById(userId);
        return TUser;
    }




    /**
     * 查询全部信息
     * @CreateTime 2019/6/8 16:35
     * @Param  userId  用户ID
     * @Return List<TUser> 用户实体集合
     */
    @RequestMapping("/getList")
    public List<TUser> getList(){
        List<TUser> TUserList = userService.list();
        return TUserList;
    }
    /**
     * 分页查询全部数据
     * @CreateTime 2019/6/8 16:37
     * @Return IPage<TUser> 分页数据
     */
    @RequestMapping("/getUserListPage")
    public IPage<TUser> getInfoListPage(int pageNum, int pageSize){
        //需要在Config配置类中配置分页插件
        IPage<TUser> page = new Page<>();
        //当前页
        page.setCurrent(pageNum);
        //每页条数
        page.setSize(pageSize);

        page = userService.page(page);
        return page;
    }



    /**
     * 根据指定字段查询用户信息集合
     * @CreateTime 2019/6/8 16:39
     * @Return Collection<TUser> 用户实体集合
     */
    @RequestMapping("/getListMap")
    public Collection<TUser> getListMap(){
        Map<String,Object> map = new HashMap<>();
        //kay是字段名 value是字段值
        map.put("phone","18792943389");
        Collection<TUser> TUserCollection = userService.listByMap(map);
        return TUserCollection;
    }



    /**
     * 新增用户信息
     * @CreateTime 2019/6/8 16:40
     */
    @RequestMapping("/saveUser")
    public void saveInfo(){

        TUser user = new TUser();
        user.setAccount("eee");
        user.setPassword("eee123");
        user.setRealName("李书涵");
        user.setAge(20);
        user.setIdCard("415689741236789456");
        user.setPhone("15648793625");

        boolean isSave = userService.save(user);
    }




    /**
     * 批量新增用户信息
     * @CreateTime 2019/6/8 16:42
     */
    @RequestMapping("/saveUserList")
    public void saveInfoList(){
        //创建对象
        TUser TUser1 = new TUser();
        TUser1.setAccount("ccc");
        TUser1.setPassword("ccc123");
        TUser1.setRealName("黎明");
        TUser1.setAge(18);
        TUser1.setIdCard("415689741236541258");
        TUser1.setPhone("15648795236");



        TUser TUser2 = new TUser();
        TUser2.setAccount("ddd");
        TUser2.setPassword("ddd123");
        TUser2.setRealName("凯叔");
        TUser2.setAge(20);
        TUser2.setIdCard("415689741236180719");
        TUser2.setPhone("15648791456");

        //批量保存
        List<TUser> list =new ArrayList<>();
        list.add(TUser1);
        list.add(TUser2);
        userService.saveBatch(list);
    }



    /**
     * 更新用户信息
     * @CreateTime 2019/6/8 16:47
     */
    @RequestMapping("/updateUser")
    public void updateInfo(){
        //根据实体中的ID去更新,其他字段如果值为null则不会更新该字段,参考yml配置文件
        TUser TUser = new TUser();
        TUser.setId(1L);
        TUser.setRealName("张欢迎");
        userService.updateById(TUser);
    }




    /**
     * 新增或者更新用户信息
     * @CreateTime 2019/6/8 16:50
     */
    @RequestMapping("/saveOrUpdateUser")
    public void saveOrUpdate(@RequestBody TUser TUser){
        //传入的实体类userInfoEntity中ID为null就会新增(ID自增)
        //实体类ID值存在,如果数据库存在ID就会更新,如果不存在就会新增


        userService.saveOrUpdate(TUser);
    }




    /**
     * 根据ID删除用户信息
     * @CreateTime 2019/6/8 16:52
     */
    @RequestMapping("/deleteUser")
    public void deleteInfo(Long userId){
        userService.removeById(userId);
    }



    /**
     * 根据ID批量删除用户信息
     * @CreateTime 2019/6/8 16:55
     */
    @RequestMapping("/deleteUserList")
    public void deleteInfoList(){
        List<Long> userIdList = new ArrayList<>();
        userIdList.add(1L);
        userIdList.add(2L);
        userService.removeByIds(userIdList);
    }





    /**
     * 根据指定字段删除用户信息
     * @CreateTime 2019/6/8 16:57
     */
    @RequestMapping("/deleteUserMap")
    public void deleteInfoMap(){
        //kay是字段名 value是字段值
        Map<String,Object> map = new HashMap<>();
        map.put("real_name","张欢迎");
        map.put("account","aaa");
        userService.removeByMap(map);
    }

}
